<?php
namespace Admin\Controller;

class CategoryController extends CommonController{
	public function add()
	{
		if(IS_GET){
			//显示添加页面
			$model = D("Category");
			$data = $model->getCateTree();
			$this->assign("data",$data);
			$this->display();
		}else{
			//处理添加
			$model = D("Category");
			$date = $model->create();

			if(!$date){
				$this->error($model->getError());
			}
			$insertId = $model->add($date);
			if(!$insertId){
				$this->error("数据写入失败");
			}
			$this->success("数据写入成功");
		}
	}
	public function index(){
		//显示分类主页
		$model = D("Category");
		$data = $model->getCateTree();
		$this->assign("data",$data);
		$this->display();
	}
	public function edit(){
		if(IS_GET){
			//显示当前ID的父权限
			$id = intval(I("get.id"));
			if($id<=0){
				$this->error("参数不对");
			}

			$model = D("Category");
			$info = $model->findOneById($id);
			$cate = $model->getCateTree();
			$this->assign("info",$info);
			$this->assign("cate",$cate);
			$this->display();
		}else{
			$model = D("Category");
			$data = $model->create();
			if(!$data){
				$this->error($model->getError());
			}
			$res = $model->update($data);
			if(!$res){
				$this->error("修改失败");
			}
			$this->success("修改成功");
		}
	}
	public function dels(){
		$id = intval(I("get.id"));
		if($id<=0){
			$this->error("参数不对");
		}
		$model = D("Category");
		$res = $model->dels($id);
		if($res === false){
			$this->error("删除失败");
		}
		$this->success("删除成功",Category/index);
	}
}


?>